package com.yazi.mxz.behavior.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class BaseController {
	
	/**
	 * 用户cookie key
	 */
	public static final String COOKIE_KEY_USER_ID = "cookie_key_user_id";
	
	
	public Cookie getCookie(String key, HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(key)) {
					return cookie;
				}
			}
		}
		return null;
	}

	public void setCookie(String key, String value, HttpServletResponse response) {
		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(Integer.MAX_VALUE);
		cookie.setPath("/");
		if (null == value) {
			cookie.setMaxAge(0);
		}
		response.addCookie(cookie);
	}

}
